<?php
/**
 * Created by PhpStorm.
 * User: as
 * Date: 2018/10/21
 * Time: 23:53
 */

namespace app\admin\controller;


use app\admin\model\Coupon;
use app\common\controller\ReturnCode;
use think\Controller;
use think\Request;

class Voucher extends Controller
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this -> aid = $request -> param('aid');
        $this -> token = $request -> param('token');
        $this -> hasToken = hasToken($this->aid,$this->token);
        $this -> coupon = $request -> param('coupon');
        $this -> couponType = $request -> param('couponType','1','trim');
//        $this -> show = $request -> param('shop','任何情况下','trim');
        $this -> rule = $request -> param('rule',0,'trim');
        $this -> startTime = $request -> param('startTime');
        $this -> endTime = $request -> param('endTime');
        $this -> couponId = $request -> param('couponid');

    }

    //列表
    public function index(){
        if(!isset($this->aid) || empty($this->token)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){
            $res = Coupon::all();
            if($res){
                return json(['status'=>0,'msg'=>ReturnCode::$return_code[0],'data'=>$res]);
            }
            return json(['status'=>5,'msg'=>ReturnCode::$return_code[5]]);
        }
        return json(['status'=>3,'msg'=>ReturnCode::$return_code[3]]);
    }
    //添加
    public function add(){
        if(!isset($this->aid) || empty($this->token) || empty($this->coupon) || empty($this->startTime) || empty($this->endTime)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){
            $data = [
                'coupon_uuid'=>uuid(),
                'coupon'=>$this->coupon,
                'coupon_type' => $this->couponType,
//                'coupon_show'=>$this->show,
                'coupon_rule'=>$this->rule,
                'start_time'=>strtotime($this->startTime),
                'end_time'=>strtotime($this->endTime),
            ];
            $has = Coupon::where($data)->find();
            if($has){
                return json(['status'=>23,'msg'=>ReturnCode::$return_code[23]]);
            }
            $res = Coupon::create($data)->allowField(true);
            if($res){
                return json(['status'=>0,'msg'=>ReturnCode::$return_code[0]]);
            }
            return json(['status'=>5,'msg'=>ReturnCode::$return_code[5]]);
        }
        return json(['status'=>3,'msg'=>ReturnCode::$return_code[3]]);
    }
    //详情
    public function info(){
        if(!isset($this->aid) || empty($this->token) || empty($this->couponId)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){
           $where = ['coupon_id' => $this->couponId];
            $res = Coupon::where($where)->find();
            if($res){
                return json(['status'=>0,'msg'=>ReturnCode::$return_code[0],'data'=>$res]);
            }
            return json(['status'=>5,'msg'=>ReturnCode::$return_code[5]]);
        }
        return json(['status'=>3,'msg'=>ReturnCode::$return_code[3]]);
    }
    //修改
    public function up(){
        if(!isset($this->aid) || empty($this->token) || empty($this->couponId)|| empty($this->coupon) || empty($this->startTime) || empty($this->endTime)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){
            $data = [
                'coupon_uuid'=>uuid(),
                'coupon'=>$this->coupon,
                'coupon_type' => $this->couponType,
//                'coupon_show'=>$this->show,
                'coupon_rule'=>$this->rule,
                'start_time'=>strtotime($this->startTime),
                'end_time'=>strtotime($this->endTime),
            ];
            $has = Coupon::where($data)->find();
            if($has){
                return json(['status'=> 23,'msg'=>ReturnCode::$return_code[23]]);
            }
            $where = ['coupon_id' => $this->couponId];
            $res = Coupon::update($data,$where);
            if($res){
                return json(['status'=>0,'msg'=>ReturnCode::$return_code[0]]);
            }
            return json(['status'=>5,'msg'=>ReturnCode::$return_code[5]]);
        }
        return json(['status'=>3,'msg'=>ReturnCode::$return_code[3]]);
    }
    //删除
    public function del(){
        if(!isset($this->aid) || empty($this->token) || empty($this->couponId)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){
            $where = ['coupon_id' => $this->couponId];
            $has = Coupon::where($where)->find();
            if(!$has){
                return json(['status' => 10, 'msg' => ReturnCode::$return_code[10]]);
            }
            $res = Coupon::destroy($where);
            if($res){
                return json(['status'=>0,'msg'=>ReturnCode::$return_code[0]]);
            }
            return json(['status'=>5,'msg'=>ReturnCode::$return_code[5]]);
        }
        return json(['status'=>3,'msg'=>ReturnCode::$return_code[3]]);
    }

}